package com.xbongbong.pro.statistics.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.elasticsearch.util.XbbElasticsearchRestTemplate;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.help.CustomHelp;
import com.xbongbong.pro.statistic.model.BiProWareModel;
import com.xbongbong.pro.statistic.model.ChartCategoryModel;
import com.xbongbong.pro.statistic.model.ChartCustomModel;
import com.xbongbong.pro.statistic.model.ChartModel;
import com.xbongbong.pro.statistic.pojo.dto.TransChartDTO;
import com.xbongbong.pro.statistic.service.BiProWareService;
import com.xbongbong.pro.statistic.service.ChartScriptService;
import com.xbongbong.pro.statistic.service.DataWarehouseService;
import com.xbongbong.saas.model.IndexSetModel;
import com.xbongbong.saas.service.toolbox.help.UpdatePackageRedisService;
import com.xbongbong.sys.model.CompanyModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 梁鲁江
 * @version v1.0
 * @date 2020/5/26 14:31
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/ckScript")
public class CKScriptController {
    private static final Logger LOG = LoggerFactory.getLogger(CKScriptController.class);
    @Resource
    private DataWarehouseService dataWarehouseService;
    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private ProBaseConfig proBaseConfig;
    @Resource
    private ChartCustomModel chartCustomModel;
    @Resource
    private ChartModel chartModel;
    @Resource
    private CustomHelp customHelp;
    @Resource
    private BiProWareModel biProWareModel;
    @Resource
    private XbbElasticsearchRestTemplate xbbElasticsearchRestTemplate;
    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private UpdatePackageRedisService updatePackageRedisService;
    @Resource
    private PaasFormExplainModel paasFormExplainModel;
    @Resource
    private ThreadPoolTaskExecutor biInitThreadPool;
    @Resource
    private BiProWareService biProWareService;
    @Resource
    private ChartScriptService chartScriptService;
    @Resource
    private ChartCategoryModel chartCategoryModel;
    @Resource
    private CompanyModel companyModel;
    @Resource
    private IndexSetModel indexSetModel;


    /**
     * 修复多表双轴图没有originalAttr，全量刷，不限制公司
     *
     * @param transChartDTO
     * @param br
     * @return java.lang.String
     * @throws XbbException
     * @author xinpeng.jiang
     * @date 2022/2/23 15:44
     * @version 1.0
     */
    @RequestMapping(value = "/repair/originalAttr", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String repairOriginalAttr(@RequestBody @Valid TransChartDTO transChartDTO, BindingResult br) {
        XbbResponse<String> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // ----------------------------------repeat post----------------------------------------------
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(transChartDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(transChartDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            // ----------------------------------repeat post----------------------------------------------
            try {
                //验证码
                if (!proBaseConfig.getXbbCode().equals(transChartDTO.getXbbCode())) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                chartScriptService.repairOriginalAttr(transChartDTO);
                response = new XbbResponse<>("操作成功");
                LOG.warn("操作成功");
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
